clear all

**Save to update later
global BS_fold_old = "$BS_fold"
global working_old = "$working"
global tempfold_old = "$tempfold"
global clean_old = "$clean"

global spec = "new_kl"
local spec = "$spec"

global tempfold = "$BS_fold/local1/" // **UPDATING TEMPFOLD TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;
global workingnew = "$BS_fold/local1/inputs/"
global BSfoldnew = "$BS_fold/local1/final/"
global cleannew = "$BS_fold/local1/inputs/"

use "$working/prowess_wits_estimation_ronly_kl.dta",clear 
save "$workingnew/prowess_wits_estimation_ronly_kl.dta", replace

use "$working/first_last_years.dta",clear 
save "$workingnew/first_last_years.dta", replace

use "$working/wholesale_priceindex.dta",clear 
save "$workingnew/wholesale_priceindex.dta", replace

global BS_fold = "$BSfoldnew" //  **UPDATING BS_fold TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;
global working = "$workingnew" //  **UPDATING working TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;


**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D"
global spec = "new_kl"
local spec = "$spec"



********************POINT ESTIMATES********************************************
do "$code/estimation/baseEst/01_prod_est_ronly_kl_opf.do"

global saveoutputs = "$working"
do "$code/estimation/flexible/data_work_R_kl.do"  // Putting together TFP terms

global datafiles = "decomposition_SS_ProdR_new_kl" 
do "$code/estimation/flexible/crosscheck.do"

global datafilesfirm= "firm_SS_ProdR_new_kl_allobs"
global datafiles = "decomposition_SS_ProdR_new_kl" 
global samplename = "ProdR"
do "$code/estimation/flexible/reg_shell.do" 

**welfare
do "$code/estimation/flexible/welfare_regs_Rkl.do" 

********************POINT ESTIMATES********************************************


***GRAB BS SAMPLES
do "$code/estimation/grabBS/01_R_kl_grabBS.do"



***RUN BS
local bsamplesize = 100

forvalues bs = 1(1)`bsamplesize' {
	*******Decomposition Sample*******
	global bs = `bs'
	
	clear 
	use "$BS_fold_old/record/Rkl/base/bssample_`bs'"
	
	save "$tempfold/bssample1", replace
	*Sample for second step (firm year)
	bysort co_code1 bs_co_code1 year: generate count_thing = _n
	keep if count_thing==1
	keep co_code1 bs_co_code1 year
	save "$tempfold/bssample2", replace

	do "$code/estimation/flexible/prod_est_ronly_kl_opf_bs.do"
	
	global saveoutputs = "$tempfold"
	do "$code/estimation/flexible/data_work_R_kl.do"
	global datafiles = "decomposition_SS_ProdR_new_kl" 
	do "$code/estimation/flexible/reg_shell_bs.do" 
	
	do "$code/estimation/flexible/welfare_regs_Rkl_bs.do"
	

	*******Full Sample*******
	clear 
	use "$BS_fold_old/record/Rkl/allobs/bssample_`bs'"
	
	save "$tempfold/bssample1", replace
	*Sample for second step (firm year)
	bysort co_code1 bs_co_code1 year: generate count_thing = _n
	keep if count_thing==1
	keep co_code1 bs_co_code1 year
	save "$tempfold/bssample2", replace
	
	do "$code/estimation/flexible/prod_est_ronly_kl_opf_bs.do"

	global saveoutputs = "$tempfold"
	do "$code/estimation/flexible/data_work_R_kl.do"

	global datafilesfirm = "firm_SS_ProdR_new_kl_allobs" 
	do "$code/estimation/flexible/reg_shell_firm_bs.do"
	
}

global BS_fold = "$BS_fold_old" //  **UPDATING BS_fold TO BE THE MAIN PATH AGAIN;
global working = "$working_old" //  **UPDATING working TO BE THE MAIN PATH AGAIN;
global tempfold = "$tempfold_old"
global clean = "$clean_old"
